%%--------------------------------------------------------------------
%% Author: <vjache>
%% Created: 02.04.2009
%% Description: Utility for pumping data from iterator to pipe.
%%--------------------------------------------------------------------
-module(pump).

%%--------------------------------------------------------------------
%% Include files
%%--------------------------------------------------------------------
-include("../include/pgt.hrl").
-include("../include/util.hrl").
%%--------------------------------------------------------------------
%% Exported Functions
%%--------------------------------------------------------------------
-export([
		 new/2,
		 init/1,
		 to_pgt_worker/3,
		 to_pgt_worker/2
		 ]).

%%--------------------------------------------------------------------
%% API Functions
%%--------------------------------------------------------------------

new(Iter,Pipe) ->
	{Iter,Pipe}.

init({Iter,Pipe}=_Pump) ->
	IterState=iter:open(Iter),
	PipeState=pipe:open(Pipe),
	{IterStateFin,PipeStateFin}=pipe:push_each(PipeState, IterState),
	ok=iter:close(IterStateFin),
	ok=pipe:close(PipeStateFin),
	ok.

to_pgt_worker(Id,{_Iter,_Pipe}=Pump) ->
	to_pgt_worker(Id,{_Iter,_Pipe}=Pump,node()).

to_pgt_worker(Id,{_Iter,_Pipe}=Pump,Node) ->
	#worker{id=Id,spawn_spec={Node,?MODULE,init,[Pump]}}.


%%--------------------------------------------------------------------
%% Test Functions
%%--------------------------------------------------------------------



%%--------------------------------------------------------------------
%% Local Functions
%%--------------------------------------------------------------------